package org.rico.quartz.dynamic;

import org.quartz.CronTrigger;
import org.quartz.Trigger;

import java.text.ParseException;

/**
 * Created by rico on 2017/9/25.
 */
public class TriggerBuilder {

    private static final String DEFAULT_JOB_NAME = "";
    private static final String DEFULT_GROUP_NAME = "";

    private String jobName = DEFAULT_JOB_NAME;
    private String groupName = DEFULT_GROUP_NAME;

    private String cronExpression;
    private CronTrigger trigger;

    public TriggerBuilder() {
        trigger = new CronTrigger();
    }

    public TriggerBuilder(String jobName, String groupName) {
        this.jobName = jobName;
        this.groupName = groupName;
        trigger = new CronTrigger();
    }

    public TriggerBuilder jobName(String jobName) {
        this.jobName = jobName;
        return this;
    }
    public TriggerBuilder groupName(String groupName) {
        this.groupName = groupName;
        return this;
    }
    public TriggerBuilder cronExpression(String cronExpression) {
        this.cronExpression = cronExpression;
        return this;
    }

    public Trigger build() throws ParseException {
        trigger.setName(jobName);
        trigger.setGroup(groupName);

        trigger.setJobName(jobName);
        trigger.setJobGroup(groupName);

        trigger.setCronExpression(cronExpression);

        return trigger;
    }

}
